package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.base.MBActivity
import com.stars.home.config.ConfigRx
import com.stars.home.databinding.ActivityPayBinding
import com.stars.home.model.PlaceOrder
import com.stars.home.vmodel.PayVModel
import com.trailer.base.utils.UiUtils


/**
 *
 *on 2021/3/1
 */
class PayActivity : MBActivity<PayVModel, ActivityPayBinding>(){


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        setShow()
        vm.ll=binding.ll
        vm.order_id=intent?.extras?.getString("order_id")
        vm.totalValue.value=intent?.extras?.getString("price")
        if(TextUtils.isEmpty(vm.order_id)) {
            var data = intent?.extras?.getString("bean")
            vm.bean= Gson().fromJson(data, PlaceOrder::class.java)

        }
    }


    private fun  setShow(){

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { //不支持沉浸式状态栏
        } else {
            //获取状态栏的高度
            val statusBarHeight: Int = UiUtils.getStatusBarHeight()

            //动态设置高度  tv
            val params = binding.tv.getLayoutParams()
            //获取当前控件的布局对象
            //params.height=UiUtils.dp2dip(getContext(), statusBarHeight);//设置当前控件布局的高度
            params.height = statusBarHeight
            binding.tv.layoutParams = params //将设置好的布局参数应用到控件中
            binding.tv.visibility = View.VISIBLE
        }

    }

    override fun listeners() {
        super.listeners()
        binding.imgBack.onClick {
            finish()
        }
    }

    override fun vmClass(): Class<PayVModel> {
        return PayVModel::class.java
    }
    override fun binding(): ActivityPayBinding {
        return ActivityPayBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override fun transluent(): Boolean {
        return true
    }

    override val isRxbus: Boolean
        get() = true


    @Subscribe(tags = [Tag(ConfigRx.PAY_COMPLETE)])
    fun setPayComplete(type: String) {
        if (vm.choseType != 3) {

            RxBus.get().post(ConfigRx.REFRESH_PAY, "")

            val intent = Intent()
            var bundle=Bundle()
            bundle.putString("order_id",vm.order_id)
            intent.putExtras(bundle)
            setResult(Activity.RESULT_OK, intent)
            finish()


        }
    }

    @Subscribe(tags = [Tag(ConfigRx.PAY_ERROR)])
    fun setPayError(type: String) {
        if (vm.choseType != 3) {

            RxBus.get().post(ConfigRx.REFRESH_PAY, "")

            val intent = Intent()
            var bundle=Bundle()
            bundle.putString("order_id",vm.order_id)
            intent.putExtras(bundle)
            setResult(Activity.RESULT_OK, intent)
            finish()


        }
    }


}
